<?php
/* $Id$ */
/*
    index.php
	Copyright (C) 2009 Wang Zhongliang <wzlsh629@163.com>.
	All rights reserved.

    Copyright (C) 2004, 2005 Scott Ullrich
    All rights reserved.

    Originally part of m0n0wall (http://m0n0.ch/wall)
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    oR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

##|+PRIV
##|*IDENT=page-system-login/logout
##|*NAME=System: Login / Logout page
##|*DESCR=Allow access to the 'System: Login / Logout' page.
##|*MATCH=index.php*
##|-PRIV


	## Load Essential Includes
	require_once('guiconfig.inc');
	require_once('notices.inc');

	## Load Functions Files
	require_once('includes/functions.inc.php');
	
	## Check to see if we have a swap space,
	## if true, display, if false, hide it ...
	if(file_exists("/usr/sbin/swapinfo")) {
		$swapinfo = `/usr/sbin/swapinfo`;
		if(stristr($swapinfo,'%') == true) $showswap=true;
	}

	## Find out whether there's hardware encryption or not
	unset($hwcrypto);
	$fd = @fopen("{$g['varlog_path']}/dmesg.boot", "r");
	if ($fd) {
		while (!feof($fd)) {
			$dmesgl = fgets($fd);
			if (preg_match("/^hifn.: (.*?),/", $dmesgl, $matches) or preg_match("/^safe.: (.*?),/", $dmesgl, $matches) or preg_match("/^ubsec.: (.*?),/", $dmesgl, $matches) or preg_match("/^padlock.: (.*?),/", $dmesgl, $matches) or preg_match("/^glxsb.: (.*?),/", $dmesgl, $matches)) {
				$hwcrypto = $matches[1];
				break;
			}
		}
		fclose($fd);
	}

	if ($_GET['if']) {
		$interface = $_GET['if'];
		if ($_GET['action'] == "Disconnect" || $_GET['action'] == "Release") {
			interface_bring_down($interface);
		} else if ($_GET['action'] == "Connect" || $_GET['action'] == "Renew") {
			interface_configure($interface);
		}
		header("Location: dashboard.php");
		exit;
	}
	
## Set Page Title and Include Header
$pgtitle = array("{$g['product_name']} 运行状态");
include("head.inc");
?>

<?php
include("fbegin.inc");
?>
<div class="soft_cont_right_table">
<table width="100%" cellspacing="0" cellpadding="6" border="0" align="center">
	<tr>
		<td class="listtopic">系统状态</td>
	</tr>
</table>
<?php
include("widgets/widgets/system_information.widget.php");
?>
<table>
	<tr>
		<td class="list" height="12"></td>
	</tr>
</table>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <?php $i = 0; $ifdescrs = get_configured_interface_with_descr(false, true);
		foreach ($ifdescrs as $ifdescr => $ifname):
			$ifinfo = get_interface_info($ifdescr);
		?>
              <?php if ($i): ?>
              <tr>
			<td colspan="8" class="list" height="12"></td>
			</tr>
		<?php endif; ?>
              <tr>
                <td colspan="2" class="listtopic">
                  <?=htmlspecialchars($ifname);?>
				  网络接口
				  (<?=($ifinfo['hwif']);?>)
				</td>
              </tr>
              <tr>
                <td width="22%" class="vncellt">状态</td>
                <td width="78%" class="listr">
                  <?=get_status_desc($ifinfo['status']);?>
                </td>
              </tr><?php if ($ifinfo['dhcplink']): ?>
		  <tr>
			<td width="22%" class="vncellt">DHCP</td>
			<td width="78%" class="listr">
			  <?=get_status_desc($ifinfo['dhcplink']);?>&nbsp;&nbsp;
			  <?php if ($ifinfo['dhcplink'] == "up"): ?>
			  <input type="button" name="<?php echo $ifdescr; ?>" value="释放" class="formbtns" onclick="window.location.href='dashboard.php?action=Release&if=<?php echo $ifdescr; ?>';">
			  <?php else: ?>
			  <input type="button" name="<?php echo $ifdescr; ?>" value="续租" class="formbtns" onclick="window.location.href='dashboard.php?action=Renew&if=<?php echo $ifdescr; ?>';">
			  <?php endif; ?>
			</td>
		  </tr><?php endif; if ($ifinfo['pppoelink']): ?>
              <tr>
                <td width="22%" class="vncellt">PPPoE</td>
                <td width="78%" class="listr">
                  <?=get_status_desc($ifinfo['pppoelink']);?>&nbsp;&nbsp;
			  <?php if ($ifinfo['pppoelink'] == "up"): ?>
			  <input type="button" name="<?php echo $ifdescr ?>" value="断开" class="formbtns" onclick="window.location.href='dashboard.php?action=Disconnect&if=<?php echo $ifdescr; ?>';">
			  <?php else: ?>
			  <input type="button" name="<?php echo $ifdescr; ?>" value="连接" class="formbtns" onclick="window.location.href='dashboard.php?action=Connect&if=<?php echo $ifdescr; ?>';">
			  <?php endif; ?>
			    </td>
              </tr><?php  endif; if ($ifinfo['pptplink']): ?>
              <tr>
                <td width="22%" class="vncellt">PPTP</td>
                <td width="78%" class="listr">
                  <?=get_status_desc($ifinfo['pptplink']);?>&nbsp;&nbsp;
			  <?php if ($ifinfo['pptplink'] == "up"): ?>
			  <input type="button" name="<?php echo $ifdescr; ?>" value="断开" class="formbtns" onclick="window.location.href='dashboard.php?action=Disconnect&if=<?php echo $ifdescr; ?>';">
			  <?php else: ?>
			  <input type="button" name="<?php echo $ifdescr; ?>" value="连接" class="formbtns" onclick="window.location.href='dashboard.php?action=Connect&if=<?php echo $ifdescr; ?>';">
			  <?php endif; ?>
                </td>
              </tr><?php  endif; if ($ifinfo['macaddr']): ?>
              <tr>
                <td width="22%" class="vncellt">MAC地址</td>
                <td width="78%" class="listr">
                  <?=htmlspecialchars($ifinfo['macaddr']);?>
                </td>
              </tr>
		<?php endif; if ($ifinfo['status'] != "down"): ?>
			  <?php if ($ifinfo['dhcplink'] != "down" && $ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down"): ?>
			  <?php if ($ifinfo['ipaddr']): ?>
              <tr>
                <td width="22%" class="vncellt">IP地址</td>
                <td width="78%" class="listr">
                  <?=implode('&nbsp;&nbsp;&nbsp;&nbsp;', $ifinfo['ipaddr_all']);?>
                  &nbsp; </td>
              </tr><?php endif; ?><?php if ($ifinfo['subnet']): ?>
              <tr>
                <td width="22%" class="vncellt">子网掩码</td>
                <td width="78%" class="listr">
                  <?=implode('&nbsp;&nbsp;&nbsp;&nbsp;', $ifinfo['subnet_all']);?>
                </td>
              </tr><?php endif; ?><?php if ($ifinfo['gateway']): ?>
              <tr>
                <td width="22%" class="vncellt">网关</td>
                <td width="78%" class="listr">
			<?=htmlspecialchars($ifinfo['gateway']);?>
			<?php /* FIXME: possibly show gateway status and if it is the default route */ ?>
                </td>
              </tr><?php endif; if ($ifdescr == "wan" && file_exists("{$g['varetc_path']}/resolv.conf")): ?>
                <td width="22%" class="vncellt">DNS服务器</td>
                <td width="78%" class="listr">
		<?php
			$dns_servers = get_dns_servers();
			foreach($dns_servers as $dns) {
				echo "{$dns}<br>";
			}
		?>
		</td>
			  <?php endif; endif; if ($ifinfo['media']): ?>
              <tr>
                <td width="22%" class="vncellt">媒介类型</td>
                <td width="78%" class="listr">
                  <?= $ifinfo['media'] == 'none' ? '无' : htmlspecialchars($ifinfo['media']);?>
                </td>
              </tr><?php endif; ?><?php if ($ifinfo['channel']): ?>
              <tr>
                <td width="22%" class="vncellt">频道</td>
                <td width="78%" class="listr">
                  <?=htmlspecialchars($ifinfo['channel']);?>
                </td>
              </tr><?php endif; ?><?php if ($ifinfo['ssid']): ?>
              <tr>
                <td width="22%" class="vncellt">网络名称(SSID)</td>
                <td width="78%" class="listr">
                  <?=htmlspecialchars($ifinfo['ssid']);?>
                </td>
              </tr><?php endif; ?>
              <tr>
                <td width="22%" class="vncellt">总数据包(in/out)</td>
                <td width="78%" class="listr">
				<?php
					echo htmlspecialchars($ifinfo['inpkts'] . "/" . $ifinfo['outpkts'] . " (");
					echo htmlspecialchars(format_bytes($ifinfo['inbytes']) . "/" . format_bytes($ifinfo['outbytes']) . ")");
				?>
                </td>
              </tr>
              <tr>
                <td width="22%" class="vncellt">通过数据包(in/out)</td>
                <td width="78%" class="listr">
				<?php
					echo htmlspecialchars($ifinfo['inpktspass'] . "/" . $ifinfo['outpktspass'] . " (");
					echo htmlspecialchars(format_bytes($ifinfo['inbytespass']) . "/" . format_bytes($ifinfo['outbytespass']) . ")");
				?>
                </td>
              </tr>
              <tr>
                <td width="22%" class="vncellt">阻止数据包(in/out)</td>
                <td width="78%" class="listr">
				<?php
					echo htmlspecialchars($ifinfo['inpktsblock'] . "/" . $ifinfo['outpktsblock'] . " (");
					echo htmlspecialchars(format_bytes($ifinfo['inbytesblock']) . "/" . format_bytes($ifinfo['outbytesblock']) . ")");
				?>
                </td>
              </tr><?php if (isset($ifinfo['inerrs'])): ?>

              <tr>
                <td width="22%" class="vncellt">错误数据包(in/out)</td>
                <td width="78%" class="listr">
                  <?=htmlspecialchars($ifinfo['inerrs'] . "/" . $ifinfo['outerrs']);?>
                </td>
              </tr><?php endif; ?><?php if (isset($ifinfo['collisions'])): ?>
              <tr>
                <td width="22%" class="vncellt">冲突</td>
                <td width="78%" class="listr">
                  <?=htmlspecialchars($ifinfo['collisions']);?>
                </td>
              </tr><?php endif; ?>
	      <?php endif; ?>

		  <?php if ($ifinfo['bridge']): ?>
		  <tr>
		    <td width="22%" class="vncellt">桥 (<?=$ifinfo['bridgeint']?>)</td>
		    <td width="78%" class="listr">
		      <?=$ifinfo['bridge'];?>
		    </td>
		  </tr>
		  <?php endif; ?>

	<?php if(file_exists("/usr/bin/vmstat")): ?>
	<?php
			$real_interface = "";
			$interrupt_total = "";
			$interrupt_sec = "";
			$real_interface = $ifinfo['hwif'];
		/* XXX for virtual interfaces this are not the most accurate stats! */
          	$interrupt_total = `vmstat -i | grep $real_interface | awk '{ print $3 }'`;
          	$interrupt_sec = `vmstat -i | grep $real_interface | awk '{ print $4 }'`;
          	if(strstr($interrupt_total, "hci")) {
    	      	$interrupt_total = `vmstat -i | grep $real_interface | awk '{ print $4 }'`;
	          	$interrupt_sec = `vmstat -i | grep $real_interface | awk '{ print $5 }'`;          	
          	}
			unset($interrupt_total); // XXX: FIX ME!  Need a regex and parse correct data 100% of the time.
	?>
	<?php if($interrupt_total): ?>
     <tr>
        <td width="22%" class="vncellt">中断</td>
        <td width="78%" class="listr">
          <?php

          	echo "总计 " . $interrupt_total;
          	echo "<br/>";
          	echo "每秒 " . $interrupt_sec;
          ?>
        </td>
      </tr>
     <?php endif; ?>
	<?php endif; ?>
	
              <?php $i++; endforeach; ?>
            </table>
          </div>
<?php include("fend.inc"); ?>
</body>
</html>